package com.agileplex.products.acc.generators;

import java.util.Iterator;

import org.dom4j.Element;

import com.agileplex.products.acc.Constants;
import com.agileplex.products.acc.commands.Option;
import com.agileplex.products.acc.commands.Parameter;
import com.agileplex.products.acc.commands.Parameters;


/**
 * Default acc options generator.
 *
 * @creator Rui Tang
 * @date 2008-06-18
 *
 * @LastChanged
 * @LastChangedBy $LastChangedBy: tangrui@agileplex.com $
 * @LastChangedDate $LastChangedDate: 2008-06-22 16:08:49 +0000 (Sun, 22 Jun 2008) $
 * @LastChangedRevision $LastChangedRevision: 7 $
 */
public class DefaultGenerator implements IGenerator {

    @SuppressWarnings("unchecked")
    public Parameters generate(Element parametersEl) {
        Parameters params = new Parameters();

        for (Iterator<Element> it = parametersEl.elementIterator(); it.hasNext(); ) {
            Element parameterEl = it.next();

            String convertor = parameterEl.attributeValue("convertor");
            if (convertor == null) {
                convertor = Constants.DEFAULT_CONVERTOR_QUALIFIED_NAME;
            }

            Parameter param = new Parameter(convertor);
            for (Iterator<Element> it2 = parameterEl.elementIterator(); it2.hasNext(); ) {
                Element optionEl = it2.next();
                Option option = new Option(optionEl.attributeValue("name"));
                option.setAbbr(optionEl.attributeValue("abbr"));
                option.setType(optionEl.attributeValue("type"));
                param.addOption(option);
            }

            params.add(param);
        }

        return params;
    }
}
